<?php
/**
 * File.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/5/10
 * Time: 15:25
 */
namespace app\admin\controller;

use think\Controller;

abstract class File extends Controller
{
    protected function getUploadRootDir()
    {
        return ROOT_PATH . 'public'.DS.'tmp'.DS.'uploads'.DS;
    }
    protected function getUploadRoot()
    {
        return '/tmp/uploads/';
    }
    abstract protected function getFormName();
    abstract protected function getUploadChildDir();
    abstract protected function processResult($result);
    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = $this->request->file($this->getFormName());
        if($file)
        {
            $path = $this->getUploadRootDir().$this->getUploadChildDir();
            $info = $file->move($path);
            if ($info) {
                return json($this->processResult(['code'=>200,'data'=>str_replace("\\",'/',$this->getUploadRoot().$this->getUploadChildDir().$info->getSaveName())]));
            } else {
                // 上传失败获取错误信息
                return json($this->processResult(['code'=>0,'msg'=>$file->getError()]));
            }
        }
        return json($this->processResult(['code'=>0,'msg'=>'error']));
    }

}